const router = require('express').Router()
const axios = require('axios')
/**
 * 
 * @api {get} /hot hot数据
 * @apiGroup 首页相关
 * @apiDescription 返回hot数据
 * @apiVersion  1.0.0
 * 
 * @apiParam {Number} cityId=''  城市id
 * @apiParam {Number} pageNum=''  请求页数
 * @apiParam {Number} pageSize=''  请求数量
 * 
 * 
 * 
 * @apiParamExample  {type} 请求示例:
 * {   cityId:10000,
 *     pageNum:1,
 *     pageSize:10
 * }
 * 
 * @apiSuccess {Number} code 200
 * @apiSuccess {Object} data 数据信息
 * @apiSuccessExample {type} 返回示例:
 * {
 *  code: 200,
 *  data: {
 *    chiefBonus: {},
 *    coming: [],
 *    hot: [
 *  {
      filmId: 5837,
      name: '来处是归途',
      poster: 'https://pic.maizuo.com/usr/movie/8748523c32bec1f25cbc9e76f0320ce6.jpg',
      actors: [Array],
      director: '刘泽',
      category: '剧情|家庭',
      synopsis: '在外地晋剧团工作的夏天一番周折后调回到本地工作，这样她便可以帮助母亲照顾已经患阿兹海默症十年的父亲，却没想
                到母亲竟因她的归来而感到焦虑。随着父亲病情的发展，原本以备恋再桨为自汗重归己会给父母设枣禁带来温情的夏天才慢慢体会到生活的
                重量，却没想到命运的钟摆又向她袭来……',
      filmType: [Object],
      nation: '中国大陆',
      language: '',
      videoId: '',
      premiereAt: 1642118400,
      timeType: 3,
      runtime: 97,
      grade: '7.2',
      item: [Object],
      isPresale: true,
      isSale: false
    }
 *  ],
 *    total:111
 *    ...
 *  }
 * }
 * 
 */

router.get('/hot', (req, res) => {
    let { cityId, pageNum, pageSize, type } = req.query
    axios.get('https://m.maizuo.com/gateway', {
        params: {
            cityId,
            pageNum,
            pageSize,
            type
        },
        headers: {
            'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"16425891382355991425318913","bc":"620100"}',
            'X-Host': 'mall.film-ticket.film.list'
        },
    }).then(data => {
        // console.log(data.data);
        data = data.data.data
        res.send({ code: 200, data })

    })
})

/**
 * 
 * @api {get} /cinemalists 当前城市所有影院数据
 * @apiName 当前城市所有影院数据
 * @apiGroup 首页相关
 * @apiDescription 返回当前城市所有影院数据
 * @apiVersion  1.0.0
 * 
 * @apiParam {Number} cityId=''  城市id
 * @apiParam {Number} ticketFlag=''  请求标识默认1
 * @apiParam {Number} pageNum=''  请求页数
 * @apiParam {Number} pageSize=''  请求数量
 * 
 * 
 * @apiParamExample  {type} 请求示例:
 * {   cityId:10000,
 *     ticketFlag:1,
 * }
 * 
 * @apiSuccess {Number} code 200
 * @apiSuccess {Object} data 数据信息
 * @apiSuccessExample {type} 返回示例:
 * {
 *  code: 200,
 *  pageArr: {
 *   cinemas:[
  {
    cinemaId: 8119,
    name: '英皇电影城（东海缤纷店）',
    address: '深圳市福田区东海缤纷天地商场B1层',
    longitude: 114.02621045065386,
    latitude: 22.542472445357134,
    gpsAddress: '114.02621045065386:22.542472445357134',
    cityId: 440300,
    cityName: '深圳市',
    districtId: 440304,
    districtName: '福田区',
    district: { districtId: 440304, name: '福田区' },
    phone: '0755-32935088',
    telephones: [ '0755-32935088' ],
    logoUrl: 'https://pic.maizuo.comusr/8119/4c825a0a7d23df9fed5b85caad9a4bc7.jpg',
    businessTime: '',
    notice: '',
    isVisited: 0,
    lowPrice: 8900,
    Distance: 0.16443638831382237,
    eTicketFlag: 0,
    seatFlag: 1,
    ticketTypes: null
    },
 * ]
 *    ...
 *  }
 * }
 * 
 */

router.get('/cinemalists', (req, res) => {
    let { cityId, ticketFlag, pageNum, pageSize } = req.query
    axios.get('https://m.maizuo.com/gateway', {
        params: {
            cityId,
            ticketFlag
        },
        headers: {
            'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"16425891382355991425318913","bc":"440300"}',
            'X-Host': 'mall.film-ticket.cinema.list'
        }
    }).then(data => {
        let len = data.data.data.cinemas.length
        data = data.data.data
        if (!pageSize) pageSize = data.cinemas.length + 1
        pageArr = data.cinemas.splice((pageNum - 1) * pageSize, pageSize)

        res.send({ code: 200, pageArr, total: len })


    })
})


/**
 * 
 * @api {get} /cinemalists 根据经纬度获取位置
 * @apiName 根据经纬度获取位置
 * @apiGroup city相关
 * @apiDescription 返回根据经纬度获取的位置
 * @apiVersion  1.0.0
 * 
 * @apiParam {Number} x=''  当前经度
 * @apiParam {Number} y=''  当前纬度
 * 
 * 
 * @apiParamExample  {type} 请求示例:
 * {   x:"31.24916171",
 *     y:"121.48789949",
 * }
 * 
 * @apiSuccess {Number} code 200
 * @apiSuccess {Object} datas 数据信息
 * @apiSuccessExample {type} 返回示例:
 * {
 *  code: 200,
 *  result: {
    location: { lng: 121.487899, lat: 31.249162 },
    formatted_address: '上海市闸北区天潼路619号',
    business: '七浦路,海宁路,北京东路',
    addressComponent: {
      city: '上海市',
      direction: 'near',
      distance: '32',
      district: '闸北区',
      province: '上海市',
      street: '天潼路',
      street_number: '619号'
    },
    cityCode: 289
  }
 *    ...
 *  }
 * }
 * 
 */

router.get('/getlocation', (req, res) => {
    let { x, y } = req.query
    axios.get(`https://api.map.baidu.com/geocoder?location=${x},${y}&coord_type=gcj02&output=json&src=webapp.baidu.openAPIdemo`).then(data => {
        let datas = data.data.result
        res.send({ code: 200, datas })
    })
})


/**
 * 
 * @api {get} /getcitylists 获取所有城市
 * @apiName 获取所有城市
 * @apiGroup city相关
 * @apiDescription 返回所有城市
 * @apiVersion  1.0.0
 * 
 * @apiParamExample  {type} 请求示例:
 * {  
 *  '无需参数'
 * }
 * 
 * @apiSuccess {Number} code 200
 * @apiSuccess {Object} datas 数据信息
 * @apiSuccessExample {type} 返回示例:
 * {
 *  code: 200,
    cities:[
  { cityId: 110100, name: '北京', pinyin: 'beijing', isHot: 1 },
  { cityId: 120100, name: '天津', pinyin: 'tianjin', isHot: 0 },
  { cityId: 130100, name: '石家庄', pinyin: 'shijia', isHot: 0 },
  { cityId: 130200, name: '唐山', pinyin: 'tangshan', isHot: 0 },
  { cityId: 130300, name: '秦皇岛', pinyin: 'qinhuang', isHot: 0 },
  { cityId: 130400, name: '邯郸', pinyin: 'handan', isHot: 0 },
 *    ...
 *  }
 * }
 * 
 */
router.get('/getcitylists', (req, res) => {
    axios.get('https://m.maizuo.com/gateway', {
        headers: {
            'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"16425891382355991425318913","bc":"440300"}',
            'X-Host': 'mall.film-ticket.city.list'
        }
    }).then(data => {
        let cities = data.data.data.cities
        res.send({ code: 200, cities })
    })
})


/**
 * 
 * @api {get} /detail 获取电影详情
 * @apiName 电影详情
 * @apiGroup movie相关
 * @apiDescription 获取电影详情
 * @apiVersion  1.0.0
 * 
 * @apiParam {Number} filmId=''  电影Id
 * @apiParamExample  {type} 请求示例:
 * {  
 *    filmId:10086
 * }
 * 
 * @apiSuccess {Number} code 200
 * @apiSuccess {Object} datas 数据信息
 * @apiSuccessExample {type} 返回示例:
 * {
     bg:3a40aa
     top:1
     code:200
     datas: {
      filmId: 5731,
      name: '天书奇谭4K纪念版',
      poster: 'https://pic.maizuo.com/usr/movie/9918a295293608b6432f36543405c884.jpg@1024h_768w_50Q',
      actors: [Array],
      director: '王树忱|钱运达|苏秀',
      category: '动画|奇幻',
      synopsis: '天庭有珍奇书籍“天书”记录了108条法术，被锁在石龛门里三千年。负责看守天书的袁公好奇不已，便乘无人之际偷偷
                观看，甚至私下凡间将天书内容刻在石壁上，意在造福百姓。但袁公此举却触犯了天条，遭到了玉帝的惩罚。趁袁公受罚之际，三只狐狸偷
                食仙丹幻化为人形，并偷得“天书”修习法术祸害人间。危难之际，一个从蛋里跳出来的孩子“蛋生”遵从袁公引导，决定为民除害，然而
                面对强大的对手，他能否顺利完成任务，实现师父的心愿？',
      filmType: [Object],
      nation: '中国大陆',
      language: '',
      videoId: '',
      premiereAt: 1636070400,
      timeType: 3,
      runtime: 100,
      grade: '7.5',
      item: [Object],
      isPresale: false,
      isSale: false
    }
  },
}
 * 
 */
router.get('/detail', (req, res) => {
    let { filmId } = req.query
    let colorArr = ['62662F', '223653', '273A40', '66583D', '182037', '1A2E40', '66633E', '243F58', '3E664F', '666666', '296666', '662A29']
    let index = Math.floor(Math.random() * colorArr.length)
    let top = Math.floor(Math.random() * 10) + 1
    let colorStr = colorArr[index]
    axios.get('https://m.maizuo.com/gateway', {
        params: {
            filmId
        },
        headers: {
            'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"16425891382355991425318913"}',
            'X-Host': 'mall.film-ticket.film.info'
        }
    }).then(data => {
        let datas = data.data.data.film
        res.send({ code: 200, datas, bg: colorStr, top })
    })
})



/**
 * @api {get} /alldaycinema 获取可放映日期影院
 * @apiName 获取可放映日期影院
 * @apiGroup movie相关
 * @apiDescription 获取可放映日期影院
 * @apiVersion  1.0.0
 * 
 * @apiParam {Number} filmId=''  电影Id
 * @apiParam {Number} cityId=''  城市Id
 * 
 * @apiParamExample  {type} 请求示例:
 * {  
 *    filmId:10086,
 *    cityId:1008611,
 * }
 * 
 * @apiSuccess {Number} code 200
 * @apiSuccess {Object} data 数据信息
 * @apiSuccessExample {type} 返回示例:
 * {
     code:200
     data: {
        cinemaExtendList:[
            {
                "cinemaId": 1465,
                "lowPrice": 6490
            },
            {
                "cinemaId": 4343,
                "lowPrice": 3900
            },
        ],
        showCinemas:[
            {
                "cinemaList": [
                4343,
                1465,
                4339,
                4344,
                7166,
                4348,
                3000,
                11048,
                7158,
                2667,
                4338,
                4337,
                6963,
                7137,
                7258,
                7301,
                2251,
                7259
                ],
                "showDate": 1643644800
            }
 
            ]
    }
  },
}
 * 
 */
router.get('/alldaycinemas', (req, res) => {
    let { filmId, cityId } = req.query
    axios.get('https://m.maizuo.com/gateway/', {
        params: {
            filmId,
            cityId
        },
        headers: {
            'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"16425891382355991425318913","bc":"620100"}',
            'X-Host': 'mall.film-ticket.cinema.film-show-cinema'
        }
    }).then(datas => {
        let cinemaExtendList = datas.data.data.cinemaExtendList
        let showCinemas = datas.data.data.showCinemas

        res.send({ code: 200, data: { cinemaExtendList, showCinemas } })
    })
})



/**
 * @api {get} /rendercinema 获取可放映日期影院
 * @apiName 获取可放映日期影院
 * @apiGroup movie相关
 * @apiDescription 获取可放映日期影院
 * @apiVersion  1.0.0
 * 
 * @apiParam {String} cinemaIds=''  电影Id,多个用,隔开
 * @apiParam {Number} cityId=''  城市Id
 * 
 * @apiParamExample  {type} 请求示例:
 * {  
 *    cinemaIds:1,2,
 *    cityId:1008611,
 * }
 * 
 * @apiSuccess {Number} code 200
 * @apiSuccess {Object} data 数据信息
 * @apiSuccessExample {type} 返回示例:
 * {
     code:200
    cinemas[
        {
            "cinemaId": 828,
            "name": "上海大光明电影院",
            "address": "南京西路216号（黄河路南京西路）",
            "longitude": 121.47782550412937,
            "latitude": 31.238980001074523,
            "gpsAddress": "121.47782550412937:31.238980001074523",
            "cityId": 310100,
            "cityName": "上海市",
            "districtId": 310101,
            "districtName": "黄浦区",
            "district": {
            "districtId": 310101,
            "name": "黄浦区"
            },
            "phone": "021-63274260",
            "telephones": [
            "021-63274260"
            ],
            "logoUrl": "https://pic.maizuo.comusr/828/e808d5b2a2b31ce817deac7538565654.tmp",
            "businessTime": "9:00-23:00",
            "notice": "",
            "isVisited": 0,
            "lowPrice": 5000,
            "Distance": 12970.590649407584,
            "eTicketFlag": 0,
            "seatFlag": 1,
            "ticketTypes": null
        }
    ]
    }
}
 * 
 */
router.post('/rendercinema', (req, res) => {
    let { cityId, cinemaIds } = req.body
    axios({
        method: 'post',
        url: 'https://m.maizuo.com/gateway',
        data: {
            cityId,
            cinemaIds
        },
        headers: {
            'Content-Type': 'application/json; charset=UTF-8',
            'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"16425891382355991425318913","bc":"620100"}',
            'X-Host': 'mall.film-ticket.cinema.batch-cinema'
        }
    }).then(datas => {
        let cinemas = datas.data.data.cinemas
        res.send({ code: 200, cinemas })
    })
})





/**
 * @api {get} /soon 获取待映影片信息
 * @apiName 获取待映影片信息
 * @apiGroup 首页相关
 * @apiDescription 获取待映影片信息
 * @apiVersion  1.0.0
 * 
 * @apiParam {Number} cityId=''  城市Id
 * @apiParam {Number} pageNum=''  第几页数据
 * @apiParam {Number} pageSize=''  请求的数量
 * 
 * 
 * @apiParamExample  {type} 请求示例:
 * {  
 *    cityId:10086,
 *    pageNum:1,
 *    pageSize:10
 * }
 * 
 * @apiSuccess {Number} code 200
 * @apiSuccess {Object} datas 数据信息
 * @apiSuccessExample {type} 返回示例:
 * {
     code:200
     data: {
        films:[
             {
                filmId: 5866,
                name: '好想去你的世界爱你',
                poster: 'https://pic.maizuo.com/usr/movie/794ed31568d90dd4a708c75023c62717.jpg',
                actors: [ [Object], [Object], [Object], [Object], [Object] ],
                director: '孙琳',
                category: '爱情|奇幻|喜剧',
                synopsis: '世界那么大，很难找到最懂你的那一个人吗？因为一次意外，建筑师助理安易(周依然 饰) 与地球另一端的调音师高昂(施柏
                宇 饰)意外相连，从此两人的生活变得鸡飞狗跳，但渐渐地这种微妙的关系，悄然发生着改变……',
                filmType: { name: '2D', value: 1 },
                nation: '中国大陆',
                language: '',
                videoId: '',
                premiereAt: 1644796800,
                timeType: 3,
                runtime: 95,
                item: { name: '2D', type: 1 },
                isPresale: true,
                isSale: false
            }
        ]
    },
    total:11
  },
}
 * 
 */
router.get('/soon', (req, res) => {
    let { cityId, pageNum, pageSize } = req.query
    let type = 2
    axios({
        method: 'get',
        url: 'https://m.maizuo.com/gateway',
        params: {
            cityId,
            pageNum,
            pageSize,
            type
        },
        headers: {
            'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"16425891382355991425318913"}',
            'X-Host': 'mall.film-ticket.film.list'
        }
    }).then(datas => {
        let films = datas.data.data.films
        let total = datas.data.data.total
        res.send({ code: 200, films, total })
    })
})



/**
 * @api {get} /classic 获取经典影片
 * @apiName 获取经典影片信息
 * @apiGroup 首页相关
 * @apiDescription 获取经典影片信息
 * @apiVersion  1.0.0
 * 
 * @apiParam {Number} limit=''  一次取的条数
 * 
 * 
 * @apiParamExample  {type} 请求示例:
 * {  
 *    limit:10,
 * }
 * 
 * @apiSuccess {Number} code 200
 * @apiSuccess {Object} datas 数据信息
 * @apiSuccessExample {type} 返回示例:
 * {
     code:200
     data: {
        films:[
             {
                filmId: 5866,
                name: '好想去你的世界爱你',
                poster: 'https://pic.maizuo.com/usr/movie/794ed31568d90dd4a708c75023c62717.jpg',
                actors: [ [Object], [Object], [Object], [Object], [Object] ],
                director: '孙琳',
                category: '爱情|奇幻|喜剧',
                synopsis: '世界那么大，很难找到最懂你的那一个人吗？因为一次意外，建筑师助理安易(周依然 饰) 与地球另一端的调音师高昂(施柏
                宇 饰)意外相连，从此两人的生活变得鸡飞狗跳，但渐渐地这种微妙的关系，悄然发生着改变……',
                filmType: { name: '2D', value: 1 },
                nation: '中国大陆',
                language: '',
                videoId: '',
                premiereAt: 1644796800,
                timeType: 3,
                runtime: 95,
                item: { name: '2D', type: 1 },
                isPresale: true,
                isSale: false
            }
        ]
    },
    total:11
  },
}
 * 
 */
router.get('/classic', (req, res) => {
    let { limit } = req.query
    axios(`https://i.maoyan.com/ajax/moreClassicList?sortId=1&showType=3&limit=${limit}&offset=10&optimus_uuid=EB4A7DD07B3A11EC95D225E4C50915F774EC1F35A8CB47F499851D0C3CBCFA4F&optimus_risk_level=71&optimus_code=10`).then(datas => {
        let classicMovies = datas.data.classicMovies
        res.send({ code: 200, classicMovies });
    })
})



module.exports = router